import Vue from "vue" //因为是基于vue所以先配置引入vue
import Vuex from "vuex" //之后配置引入vuex
import axios from 'axios'
import {Littlehow} from "../assets/js/util";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        userList: [],
        showData: true,
        token: '',
        selectUserId: 0,
        echo: null,
        buttonGroup: null,
        btnArr: null,
        sw: null,
        infos: null,
        messList: [],
        //活动排名下一步存储数据
        activeRanking: null,
        //存放banner图数据
        bannerData: null,
        //存放大图
        bigImg: null,
        // 存放权益的数据
        powerInterestData: []
    },
    mutations: {
        set_token(state, token) {
            state.token = token
            sessionStorage.token = token
        },
        del_token(state) {
            state.token = ''
            sessionStorage.removeItem('token')
        },
        // 改变存放权益的数据的方法
        setPowerInterestData(state, data) {
            state.powerInterestData = data
        },
        //排名提交数据
        setActiveRanking(state, data) {
            state.activeRanking = data
        },
        clearSetActiveRanking(state, data) {
            state.activeRanking = null
        },
        //获取banner图数据
        setBannerData(state, data) {
            state.bannerData = data
        },
        clearSetBannerData(state, data) {
            state.bannerData = null
        },
        //获取大图
        setBingImg(state, data) {
            state.bigImg = data
        },
        clearSetBingImg(state, data) {
            state.bigImg = null
        },


        getUserId(state, dataa) {
            state.selectUserId = dataa
        },
        setEcho(state, datas) {
            state.echo = datas
        },
        getBtgGroup(state, datas) {
            console.log(datas)
            state.buttonGroup = datas
        },
        setBtnArr(state, datas) {
            state.btnArr = datas
        },
        setSw(state, num) {
            state.sw = num
        },
        setInfos(state, data) {
            state.infos = data
        },
        getCount(state, listData) {
            state.messList = listData
        },


    },
    actions: {
        getCount() {
            // alert(111)
            // debugger
            axios
                .post("/api/sys/menu/nav", {isShow: 0})
                .then(res => {
                    // alert("获取数据成功");

                    // console.log(res);
                    let listData1 = res.data.data;
                    //下面获取到的数据是
                    // console.log("下面获取到的数据是");
                    listData1.map(item => {
                        item.lable = item.name;
                        item.value = item.id;
                    });
                    if (listData1) {
                        var format = {
                            id: "id", //菜单编号，作为树形查找id
                            root: -1, //顶层节点的parentId是root
                            pid: "parentId", //父节点标识字段为parentId
                            child: "children", //构造出的树形子节点数组名为children
                            sort: {field: "sortNo", direct: 1}
                        }; //排序字段为sortNo，且正序
                        var result = Littlehow.tree.format(listData1, format);
                        var listData = result[0].children;
                        //下面是左侧列表的排序
                        for (let i = 0; i < listData.length; i++) {
                            for (let j = 0; j < listData.length - 1 - i; j++) {
                                if (listData[j].sort > listData[j + 1].sort) {
                                    var temp = listData[j + 1];
                                    listData[j + 1] = listData[j];
                                    listData[j] = temp;
                                }
                            }
                        }
                        // return listData;
                        alert(listData)
                        commit("getCount", listData)
                    }
                })
                .catch(err => {
                    // alert("失败")
                    console.log(err);
                });
        }
    }
})
